package com.sbillings.messenger.hub;

/**
 * The front door for incoming messages. All messages come through here.
 * This class' purpose is to receive the raw ingredients for a message (from, say, a servlet receiving
 * messages over the internet), validate them,
 * create a Message from them, and then pass that Message to a publisher (who will broadcast them
 * to its subscribers). If the input is invalid, it  
 * creates a message that indicates that an internal error has occurred (hopefully some admin has subscribed to that type
 * of message). 
 * 
 * Typical sources of the incoming messages: servlets receiving signals over the internet from
 * devices or application.
 * 
 * Typical publisher: an object that manages, and broadcasts messages to, subscribers.
 * 
 * @author Steve
 *
 */
public class MessageGateway {
	private Message currentMessage=null; // the current / most recent message
	private MessagePublisher publisher; // we forward messages on to this publisher
	
	public MessageGateway(MessagePublisher publisher) {
		this.publisher = publisher;
	}
	
	/**
	 * Call this method to generate a Message from the given details, and forward
	 * them to the publisher.
	 * 
	 * @param source The name of the application or device from which the message came.
	 * @param typeValue The message type (See MessageType.java).
	 * @param body The body of the message.
	 */
	public void processMessage(String source, int typeValue, String body) {
		this.currentMessage = new Message(source, MessageType.getType(typeValue), body);
		publisher.publish(this.currentMessage); // send the message to the publisher for publication
	}

	public Message getCurrentMessage() {
		return currentMessage;
	}
	
}
